package com.moco.launcher.wifi;

import com.moco.launcher.R;
import com.oazon.moco.link.core.LinkSDK;
import com.oazon.moco.serialport.SerialControll;
import com.oazon.voicelib.service.SpeechManage;

import android.annotation.SuppressLint;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.InputType;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewStub;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;

/**
 * wifi 自定义键盘输入界面
 * 
 * @author leker
 *
 */
public class WifInputFragment extends Fragment implements OnClickListener {
	private String TAG = "WifInputFragment";
	private View rootView;
	private ConnectImpl connectImpl;
	private ImageView exitImg;

	private ViewStub lowerStub, upperStub, numberStub;
	private EditText keyBoardLabel;
	private ImageButton wifi_key_change_btn, wifi_key_board_upper_btn,
			wifi_key_delete_btn, wifi_key_board_ok_btn;

	private StringBuffer mBuffer;

	public enum INPUT_TYPE {
		UPPER, LOWER, NUMBER
	}

	private INPUT_TYPE currentType;

	public WifInputFragment(ConnectImpl impl) {
		connectImpl = impl;
		mBuffer = new StringBuffer();
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		Log.i(TAG, "enter onCreateView---------");
		// return super.onCreateView(inflater, container, savedInstanceState);
		rootView = inflater.inflate(R.layout.wifi_keyboard_layout, container,
				false);
		exitImg = (ImageView) rootView.findViewById(R.id.wifi_keyboard_exit);
		exitImg.setOnClickListener(this);

		keyBoardLabel = (EditText) rootView
				.findViewById(R.id.wifi_key_input_edittext);
		keyBoardLabel.setInputType(InputType.TYPE_NULL);
		wifi_key_change_btn = (ImageButton) rootView
				.findViewById(R.id.wifi_key_change_btn);
		wifi_key_change_btn.setOnClickListener(this);
		wifi_key_board_upper_btn = (ImageButton) rootView
				.findViewById(R.id.wifi_key_board_upper_btn);
		wifi_key_delete_btn = (ImageButton) rootView
				.findViewById(R.id.wifi_key_delete_btn);
		wifi_key_board_ok_btn = (ImageButton) rootView
				.findViewById(R.id.wifi_key_board_ok_btn);
		wifi_key_board_upper_btn.setOnClickListener(this);
		wifi_key_delete_btn.setOnClickListener(this);
		wifi_key_board_ok_btn.setOnClickListener(this);

		upperStub = (ViewStub) rootView
				.findViewById(R.id.key_board_input_upper_viewstub);
		upperStub.inflate();
		lowerStub = (ViewStub) rootView
				.findViewById(R.id.key_board_input_lower_viewstub);
		lowerStub.inflate();
		numberStub = (ViewStub) rootView
				.findViewById(R.id.key_board_input_number_viewstub);
		numberStub.inflate();

		loadKeyboardStub(INPUT_TYPE.LOWER);
		initKeyBoardLayout();
		return rootView;
	}

	private void initKeyBoardLayout() {
		for (int i = 0; i < KeyBoardResource.UpperKeyBtnIds.length; i++) {
			ImageButton imgBtn = (ImageButton) rootView
					.findViewById(KeyBoardResource.UpperKeyBtnIds[i]);
			if (imgBtn != null)
				imgBtn.setOnClickListener(new KeyImageBtnClickListener());
		}

		for (int i = 0; i < KeyBoardResource.LowerKeyBtnIds.length; i++) {
			ImageButton imgBtn = (ImageButton) rootView
					.findViewById(KeyBoardResource.LowerKeyBtnIds[i]);
			if (imgBtn != null)
				imgBtn.setOnClickListener(new KeyImageBtnClickListener());
		}

		for (int i = 0; i < KeyBoardResource.NumberKeyBtnIds.length; i++) {
			ImageButton imgBtn = (ImageButton) rootView
					.findViewById(KeyBoardResource.NumberKeyBtnIds[i]);
			if (imgBtn != null)
				imgBtn.setOnClickListener(new KeyImageBtnClickListener());
		}
	}

	public class KeyImageBtnClickListener implements OnClickListener {
		@Override
		public void onClick(View v) {
			String result = KeyBoardResource.getCharById(v.getId());
			Log.i(TAG, "click char is:" + result);
			mBuffer.append(result);
			Log.i(TAG, "after mBuffer char is:" + mBuffer.toString());
			keyBoardLabel.setText(mBuffer);
		}
	}

	@SuppressLint("NewApi")
	private void loadKeyboardStub(INPUT_TYPE type) {
		upperStub.setVisibility(View.GONE);
		lowerStub.setVisibility(View.GONE);
		numberStub.setVisibility(View.GONE);

		switch (type) {
		case UPPER:
			wifi_key_change_btn.setBackground(getResources().getDrawable(
					R.drawable.wifi_key_number_123));
			upperStub.setVisibility(View.VISIBLE);
			break;
		case LOWER:
			wifi_key_change_btn.setBackground(getResources().getDrawable(
					R.drawable.wifi_key_number_123));
			lowerStub.setVisibility(View.VISIBLE);
			// lowerStub.inflate();
			break;
		case NUMBER:
			wifi_key_change_btn.setBackground(getResources().getDrawable(
					R.drawable.wifi_key_lower_abc));
			numberStub.setVisibility(View.VISIBLE);
			break;
		}
		currentType = type;
	}

	@Override
	public void onResume() {
		SerialControll.getInstance().stopControl();;
		SpeechManage.getInstance().stopWake();
		super.onResume();
	}

	private void deleteBuffer() {
		Log.i(TAG, "enter deleteBuffer()");
		if (mBuffer != null && mBuffer.length() > 0)
			mBuffer.deleteCharAt(mBuffer.length() - 1);
		keyBoardLabel.setText(mBuffer);
	}

	// 切换键盘
	public void changeKeyBoard() {
		if (currentType == INPUT_TYPE.LOWER)
			currentType = INPUT_TYPE.NUMBER;
		else if (currentType == INPUT_TYPE.UPPER)
			currentType = INPUT_TYPE.NUMBER;
		else if (currentType == INPUT_TYPE.NUMBER)
			currentType = INPUT_TYPE.LOWER;
		loadKeyboardStub(currentType);
	}

	public void changeUpperBoard() {
		if (currentType != INPUT_TYPE.UPPER) {
			loadKeyboardStub(INPUT_TYPE.UPPER);
		} else if (currentType == INPUT_TYPE.UPPER) {
			loadKeyboardStub(INPUT_TYPE.LOWER);
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.wifi_key_change_btn:
			changeKeyBoard();
			break;
		case R.id.wifi_key_board_upper_btn:
			// 切换成大写
			changeUpperBoard();
			break;
		case R.id.wifi_keyboard_exit:
			clearEditText();
			getActivity().onBackPressed();
			break;
		case R.id.wifi_key_delete_btn:
			deleteBuffer();
			break;
		case R.id.wifi_key_board_ok_btn:
			// save StringBuffer supply for other fragment
			Log.i(TAG, "");
			if(mBuffer.toString().equals("1oaozn2")){
				 ComponentName componetName = new ComponentName(
		                  //这个是另外一个应用程序的包名 
		      			"com.example.dragontest",
		                 //这个参数是要启动的Activity 
		                 "com.example.dragontest.MainActivity"); 
		         Intent intent= new Intent();
		         intent.setComponent(componetName);
		         startActivity(intent);
		         
		         
		         android.os.Process.killProcess(android.os.Process.myPid());
		      	 System.exit(0);
		       
		       
			}
			
			android.provider.Settings.System.putString(getActivity().getContentResolver(),
					ConnectImpl.INPUT_PWD, mBuffer.toString());
			clearEditText();
			connectImpl.exit(ConnectImpl.INPUT);
			break;
		default:
			break;
		}
	}

	public void clearEditText() {
		mBuffer.delete(0, mBuffer.length());
		// Log.i(TAG,
		// "keyBoardLabel is:"+keyBoardLabel+"--mBuffer is:"+mBuffer);
		if (keyBoardLabel != null)
			keyBoardLabel.setText("");
	}

	@Override
	public void onPause() {
		SerialControll.getInstance().startControl();
		SpeechManage.getInstance().startWake();
		super.onPause();
	}

	@Override
	public void onDestroyView() {
		super.onDestroyView();
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
	}

}
